package test

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"testing"

	"k8gbterratest/utils"

	"github.com/stretchr/testify/require"
)

func TestWeightsExistsInLocalDNSEndpoint(t *testing.T) {
	for _, ingressType := range utils.IngressTypes {
		const basePath = "../examples/roundrobin-weight1"
		const host = "terratest-roundrobin.cloud.example.com"

		workflowEU := utils.NewWorkflow(t, "k3d-test-gslb1", 5053)
		workflowUS := utils.NewWorkflow(t, "k3d-test-gslb2", 5054)
		t.Run(ingressType.String(), func(t *testing.T) {
			abstractTestWeightsExistsInLocalDNSEndpoint(t, host,
				workflowEU.Enrich(basePath, host, ingressType),
				workflowUS.Enrich(basePath, host, ingressType),
			)
		})
	}
}

func abstractTestWeightsExistsInLocalDNSEndpoint(t *testing.T, host string, workflowEU, workflowUS *utils.Workflow) {
	const endpointDNSNameEU = "gslb-ns-eu-cloud.example.com"
	const endpointDNSNameUS = "gslb-ns-us-cloud.example.com"
	const zone = "cloud.example.com"
	workflowEU = workflowEU.WithTestApp("eu")
	instanceEU, err := workflowEU.Start()
	require.NoError(t, err)
	defer instanceEU.Kill()

	workflowUS = workflowUS.WithTestApp("us")
	instanceUS, err := workflowUS.Start()
	require.NoError(t, err)
	defer instanceUS.Kill()

	err = instanceEU.WaitForAppIsRunning()
	require.NoError(t, err)
	err = instanceUS.WaitForAppIsRunning()
	require.NoError(t, err)

	err = instanceEU.Resources().WaitForExternalDNSEndpointHasTargets(zone, endpointDNSNameEU)
	require.NoError(t, err)
	epExternalEU, err := instanceEU.Resources().GetK8gbExternalDNSEndpoint(zone).GetEndpointByName(endpointDNSNameEU)
	require.NoError(t, err, "missing EU endpoint %s", endpointDNSNameEU)
	err = instanceUS.Resources().WaitForExternalDNSEndpointHasTargets(zone, endpointDNSNameUS)
	require.NoError(t, err)
	epExternalUS, err := instanceUS.Resources().GetK8gbExternalDNSEndpoint(zone).GetEndpointByName(endpointDNSNameUS)
	require.NoError(t, err, "missing US endpoint %s", endpointDNSNameUS)

	expectedTargets := append(epExternalEU.Targets, epExternalUS.Targets...)

	err = instanceEU.WaitForLocalDNSEndpointHasTargets(expectedTargets)
	require.NoError(t, err, "EU expectedTargets %v but has %v for endpoint %s",
		expectedTargets, epExternalEU.Targets, endpointDNSNameEU)
	err = instanceUS.WaitForLocalDNSEndpointHasTargets(expectedTargets)
	require.NoError(t, err, "US expectedTargets %v", expectedTargets)

	for _, instance := range []*utils.Instance{instanceEU, instanceUS} {
		ep, err := instance.Resources().GetLocalDNSEndpoint().GetEndpointByName(host)
		require.NoError(t, err, "missing endpoint", host)
		// check all labels are correct
		require.Equal(t, "roundRobin", ep.Labels["strategy"])
		require.NotEqual(t, ep.Labels["weight-eu-0-5"], ep.Labels["weight-eu-1-5"])
		require.NotEqual(t, ep.Labels["weight-us-0-5"], ep.Labels["weight-us-1-5"])
		// check all targets are correct
		for _, v := range epExternalEU.Targets {
			require.True(t, Contains(v, ep.Targets))
		}
		for _, v := range epExternalUS.Targets {
			require.True(t, Contains(v, ep.Targets))
		}
	}
}

func Contains(str string, values []string) bool {
	for _, v := range values {
		if str == v {
			return true
		}
	}
	return false
}
